﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.Common;
using Job.Web.Framework;

namespace Job.Web.userControls.Admin
{
    public partial class CommonDictionaryList : BaseCtrl
    {
        private int dictClass
        {            
            get { return myR["dictclass"].IntValue;}
        }

        DictionaryLogic logic = new DictionaryLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindCount();
                BindData();
            }
        }

        public void BindCount()
        {
            string strClass = Constants.Num2FixLenStr(dictClass, Constants.DICT_CLASS_LEN);
            pagerButtom.RecordCount = logic.CountCommon( strClass );
            pagerButtom.DataBind();
        }
        public void BindData()
        {
            int pageSize = pagerButtom.PageSize;
            int pageIdx = pagerButtom.CurrentPageIndex;
            string strClass = Constants.Num2FixLenStr(dictClass,Constants.DICT_CLASS_LEN);
            this.gridDictionary.DataSource = logic.GetCommonPageList( strClass,pageSize, pageIdx);
            this.gridDictionary.DataBind();
        }

        protected void gridDictionary_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    Label label = (Label)e.Row.FindControl("lbStatus");
                    if (label != null)
                    {
                        string text = label.Text;
                        if (text.Equals("true", StringComparison.OrdinalIgnoreCase))
                            label.Text = "有效";
                        else
                            label.Text = "无效";
                    }
                }
            }
        }

        protected void gridDictionary_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int row = e.RowIndex;
            Label label = (Label)gridDictionary.Rows[row].FindControl("lbId");
            string dictId = label.Text;
            if (null == dictId || dictId.Trim().Equals("")) return;
            logic.DeleteCommon(Convert.ToInt32(dictId));

            BindCount();
            BindData();

        }

        protected void gridDictionary_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int row = e.NewEditIndex;
            Label label = (Label)gridDictionary.Rows[row].FindControl("lbId");
            string dictId = label.Text;
            if (null == dictId || dictId.Trim().Equals("")) return;

            string url = "~/AdminWeb/AdminMain.aspx?controlType={0}&dictclass={1}&edit=1&id={2}";
            Response.Redirect(string.Format(url, dictClass, dictClass,dictId));
        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btNew_Click(object sender, EventArgs e)
        {
            string url = "~/AdminWeb/AdminMain.aspx?controlType={0}&dictclass={1}&edit=1";
            Response.Redirect(string.Format(url,dictClass,dictClass));
        }

    }
}